<template>
  <!--考核单位管理-->
  <a-spin :spinning="isRefresh">
    <page-list :pageOption="pageOption" ref="deptPage">
      <template #tipsBefore>
        <a-button type="primary" preIcon="ant-design:plus-outlined" @click="handleCreate">新增</a-button>
        <a-button type="primary" style="margin-left: 8px" @click="handleTest">特殊单位处理</a-button>
      </template>
    </page-list>
  </a-spin>
  <depart-modal @register="registerDeptModal" @success="departHandleSuccess" />
  <depart-test-modal @register="registerDeptTestModal" @success="departTestHandleSuccess" />
  <edit-modal @register="registerModal" @success="handleSuccess" />
</template>

<script setup>
  import PageList from '../../../components/newPageList/index.vue';
  import { render } from '/@/utils/common/renderUtils';
  import { formatName } from '/@/views/zhc/util/numUtils';
  import { editForm } from './departmentManage.data';
  import EditModal from './editModal.vue';
  import DepartTestModal from './departTestModal.vue';
  import DepartModal from './departModal.vue';
  import { useModal } from '/@/components/Modal';
  import { initDictOptions } from '/@/utils/dict/index';
  import { deleteOne, refresh } from './service';

  const isRefresh = ref(false);
  const deptPage = ref(null);
  const [registerModal, { openModal: openModal = () => {} }] = useModal();
  //注册新增弹窗
  const [registerDeptModal, { openModal: openDeptModal = () => {} }] = useModal();
  const [registerDeptTestModal, { openModal: openDeptTestModal = () => {} }] = useModal();

  const customDeptConfig = (record) => {
    openDeptModal(true, { ...record });
  };

  const departHandleSuccess = (orgCode) => {
    isRefresh.value = true;
    refresh({ suitUnit: orgCode })
      .then((res) => {
        deptPage.value.reload();
        isRefresh.value = false;
      })
      .finally(() => {
        isRefresh.value = false;
      });
  };

  const departTestHandleSuccess = (orgCode) => {
    isRefresh.value = true;
    refresh({ suitUnit: orgCode })
      .then((res) => {
        deptPage.value.reload();
        isRefresh.value = false;
      })
      .finally(() => {
        isRefresh.value = false;
      });
  };

  const handleSuccess = () => {
    deptPage.value.reload();
  };

  // const delDeptInit = () => {
  //   alert('delDeptInit');
  // };
  // const roleInit = () => {
  //   alert('roleInit');
  // };
  // const relevancyDept = () => {
  //   alert('relevancyDept');
  // };

  const customEdit = (record) => {
    openModal(true, { record, isUpdate: true });
  };
  const customDelete = (record) => {
    deleteOne({ ids: record.id }).then((res) => {
      deptPage.value?.reload();
    });
  };
  const handleTest = () => {
    openDeptTestModal(true, { isUpdate: false });
  };
  const handleCreate = () => {
    openModal(true, { isUpdate: false });
  };
  //下发方法
  provide('customDeptConfig', customDeptConfig);
  provide('customEdit', customEdit);
  provide('customDelete', customDelete);
  // provide('delDeptInit', delDeptInit);
  // provide('roleInit', roleInit);
  // provide('relevancyDept', relevancyDept);

  const statusOptionsList = ref([]);
  initDictOptions('hub_suit_type').then((res) => {
    statusOptionsList.value = res || [];
  });

  const pageOption = {
    dataUrl: '/base/suitUnit/findPage',
    useUrl: true,
    showCreate: false,
    noSelection: true,
    method: 'get',
    customActions: [
      {
        label: '考核部门配置',
        action: 'customDeptConfig',
      },
      {
        label: '编辑',
        action: 'customEdit',
      },
      {
        label: '删除',
        action: 'customDelete',
      },
    ],
    //操作列补充按钮 todo
    /*supActions: [
      {
        label: '初始化',
        action: 'deptInit',
        confirm: '确认初始化该单位吗',
        //动态禁用按钮规则
        dynamicRules: {
          key: 'isInit', //条件字段
          condition: 'unequal', //equal  等于 exceeds 大于 less 小于 unequal 不等于
          value: true, //为该值时禁用按钮
        },
      },
      {
        label: '删除初始化',
        action: 'delDeptInit',
        confirm: '确认删除该单位初始化吗',
        dynamicRules: {
          key: 'deptInit',
          condition: 'unequal',
          value: false, //为该值时禁用按钮
        },
      },
      {
        label: '初始化角色',
        action: 'roleInit',
        confirm: '您确认要初始化选择的考核单位的角色吗？系统将根据角色的适用人员类型与员工自动关联！',
        dynamicRules: {
          key: 'deptInit',
          condition: 'unequal',
          value: false, //为该值时禁用按钮
        },
      },
      {
        label: '关联考核部门',
        action: 'relevancyDept',
      },
    ],*/
    searchForm: [
      {
        field: 'suitUnitName',
        label: '单位名称',
        component: 'Input',
        colProps: { span: 8 },
      },
      {
        field: 'unitType',
        label: '考核单位类型',
        component: 'Select',
        componentProps: {
          options: statusOptionsList,
          immediate: true,
          onChange: (e) => {},
          fieldNames: {
            label: 'text',
            value: 'value',
          },
          onOptionsChange: (options) => {
            // console.log('get options', options.length, options);
          },
        },
        colProps: { span: 8 },
      },
    ],
    columns: [
      {
        title: 'id',
        dataIndex: 'id',
        width: 80,
        ifShow: false,
      },
      {
        title: '考核单位名称',
        dataIndex: 'orgName',
        width: 150,
        align: 'center',
      },
      {
        title: '考核单位编码',
        dataIndex: 'orgCode',
        width: 150,
        align: 'center',
      },
      {
        title: '考核单位简称',
        dataIndex: 'suitUnitName',
        width: 150,
        align: 'center',
      },
      // {
      //   title: '上级单位',
      //   dataIndex: 'parentCode',
      //   align: 'center',
      //   width: 120,
      //   customRender: ({ text }) => {
      //     return -1 == text ? '无' : text;
      //   },
      // },
      {
        title: '上级考核单位',
        dataIndex: 'parentName',
        width: 150,
        align: 'center',
      },
      {
        title: '绩效管理员',
        dataIndex: 'managerName',
        align: 'center',
        width: 90,
      },
      {
        title: '单位分类',
        dataIndex: 'unitType',
        customRender: ({ text }) => {
          return render.renderDict(text, 'hub_suit_type');
        },
        align: 'center',
        width: 110,
      },
      {
        title: '单位分组编码',
        dataIndex: 'stTypeCode',
        align: 'center',
        width: 110,
        ifShow: false,
      },
      {
        title: '单位分组',
        dataIndex: 'stTypeCodeName',
        align: 'center',
        width: 110,
      },
      {
        title: '是否初始化',
        dataIndex: 'isInit',
        align: 'center',
        width: 90,
        ifShow: false,
        customRender: ({ text }) => {
          // @ts-ignore
          return text ? '是' : '否';
        },
      },
      {
        title: '排序',
        dataIndex: 'displayOrder',
        align: 'center',
        width: 70,
        ifShow: false,
      },
      {
        title: '备注',
        dataIndex: 'remark',
        align: 'center',
        width: 120,
      },
    ],
    //新增编辑弹窗表单
    // modalForm: editForm,
  };
</script>

<style scoped></style>
